package com.joyoung.common.utils.singn;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Encrypt {
    public Encrypt() {
    }
    
    public static String MD5(byte[] value) {
        try {
            byte[] encs = Md5Coder.encrypt(value);
            return Hex.encodeHexString(encs).toUpperCase();
        } catch (Exception var2) {
            return "";
        }
    }
    
    public static String MD5(String data) {
        try {
            return MD5(data.getBytes("utf-8")).toUpperCase();
        } catch (Exception var2) {
            return "";
        }
    }
    
    public static String MD54AfterSale(String data) {
        try {
            return MD5(data.getBytes()).toUpperCase();
        } catch (Exception var2) {
            return "";
        }
    }
    
    public static String MD5(String data, String salt) {
        try {
            byte[] dataBuffer = data.getBytes("utf-8");
            byte[] saltBuffer = salt.getBytes("utf-8");
            return MD5(dataBuffer, saltBuffer);
        } catch (Exception var4) {
            return "";
        }
    }
    
    public static String MD5(byte[] dataBuffer, String salt) {
        try {
            byte[] saltBuffer = salt.getBytes("utf-8");
            return MD5(dataBuffer, saltBuffer);
        } catch (Exception var3) {
            return "";
        }
    }
    
    public static String MD5(byte[] dataBuffer, byte[] saltBuffer) {
        try {
            byte[] encs;
            if (saltBuffer != null && saltBuffer.length != 0) {
                encs = Md5Coder.encrypt(dataBuffer, saltBuffer, true, 0, dataBuffer.length);
            } else {
                encs = Md5Coder.encrypt(saltBuffer);
            }
            
            return Hex.encodeHexString(encs).toUpperCase();
        } catch (Exception var3) {
            return "";
        }
    }
    
    public static String encryptExOK(String keys, String data) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength += blockSize - plaintextLength % blockSize;
            }
            
            SecretKeySpec secretKeySpec = new SecretKeySpec(keys.getBytes("utf-8"), "AES");
            byte[] plaintext = new byte[plaintextLength];
            
            for(int i = 0; i < plaintextLength; ++i) {
                plaintext[i] = 0;
            }
            
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            cipher.init(1, secretKeySpec);
            byte[] encrypted = cipher.doFinal(plaintext);
            String originalString = toHex(encrypted);
            return originalString;
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }
    
    public static String toHex(byte[] buf) {
        if (buf == null) {
            return "";
        } else {
            StringBuffer result = new StringBuffer(2 * buf.length);
            
            for(int i = 0; i < buf.length; ++i) {
                appendHex(result, buf[i]);
            }
            
            return result.toString();
        }
    }
    
    private static void appendHex(StringBuffer sb, byte b) {
        String HEX = "0123456789ABCDEF";
        sb.append("0123456789ABCDEF".charAt(b >> 4 & 15)).append("0123456789ABCDEF".charAt(b & 15));
    }
    
    public static String getDataKey(String key) {
        String str = "";
        int i = 0;
        
        for(int length = key.length(); i < length; i += 2) {
            str = str + key.substring(i, i + 1);
        }
        
        return str;
    }
}

